package com.springbootes.springbootes.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import com.springbootes.springbootes.bean.ResultMsg;
import com.springbootes.springbootes.bean.TestContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @description:
 * @author: ZZZ
 */
@Controller
@RequestMapping("/test")
public class Test {

    @Autowired
    private ElasticsearchClient client;

    @RequestMapping("/add")
    public ResultMsg<Object> add(String indexName) throws IOException {
        CreateIndexResponse indexResponse = client.indices().create(c -> c.index(indexName));
        return ResultMsg.success();
    }

    public boolean existsTest() throws IOException {
        BooleanResponse booleanResponse = client.indices().exists(e -> e.index("user"));
        return booleanResponse.value();
    }

    public void test() throws IOException {
        String indexName = UUID.randomUUID().toString().replace("-", "");
        System.out.println(indexName);

        String path = "C:\\Users\\user\\Desktop\\测试2\\e55a6e0f1e2a427fb0e8808a60ccaaff-招标\\商务文件-拆分后.txt";

        List<String> list = new ArrayList<>();
        FileReader reader = new FileReader(path);
        BufferedReader bufferedReader = new BufferedReader(reader);
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        bufferedReader.close();
        int n = 0;

        String[] split = stringBuilder.toString().replace("#####", "\\$\\$\\$\\$\\$").split("\\$\\$\\$\\$\\$");
        for (String s : split) {
            if (s.trim().length() > 5) {
                TestContent testContent = new TestContent();
                testContent.setContent(s);
                n++;
                IndexResponse indexResponse = client.index(i -> i
                        .index("zb")
                        //设置id
                        //.id("1")
                        //传入user对象
                        .document(testContent));
            }
        }

    }
}
